package com.itboye.api.domain;

import com.itboye.api.model.ByResponseData;
import com.itboye.mybatis.enumtype.CodeType;
import com.itboye.mybatis.helper.BySecurityCodeHelper;
import com.itboye.mybatis.model.SecurityCodeModel;
import com.itboye.services.utils.interfaces.ISecurityCodeService;
import com.itboye.system.exception.ByBaseException;
import com.itboye.system.helper.ByDateHelper;
import com.itboye.system.helper.ByIpHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * SecurityCodeDomain.java
 * Created on  2016/7/29 12:28
 * 版本       修改时间          作者      修改内容
 * V1.0.1    2016/7/29      hebidu    初始版本
 * <p>
 * Copyright (c) 2013 杭州博也网络科技有限公司 版权所有
 * HANGZHOU BOYE TECHNOLOGY CO.,LTD. All Rights Reserved.
 */
@Component("SecurityCodeDomain")
public class SecurityCodeDomain extends BaseDomain {

    @Resource
    private  ISecurityCodeService securityCodeService;

    public SecurityCodeDomain(){
        super();
    }

    /**
     * 发送、生成验证码
     * @return
     */
    public ByResponseData send() throws ByBaseException, UnsupportedEncodingException {

        this.checkApiVersion("101");
        //mobile不一定有,
        String accepter = this.getStringParam("mobile");
        Integer codeTypeValue = this.getIntegerParam("type");
        String code = BySecurityCodeHelper.generate(4);

        assertNotNull(accepter,"缺少mobile参数");
        assertNotNull(codeTypeValue,"缺少type参数");
        CodeType codeType = CodeType.getCodeType(codeTypeValue);
        assertNotNull(codeType,"type参数无效");

        //同一手机号发送消息间隔 单位(秒)
        long maxInterval = 30;
        HashMap<String,String> params = new HashMap<String, String>();
        params.put("accepter",accepter);

        SecurityCodeModel  model = securityCodeService.getInfoBy(params);

        if(model != null){
            if(ByDateHelper.getCurrentTimeStamp() - model.getStarttime() < maxInterval){
                return fail("验证码发送过于频繁,请稍后再尝试!");
            }
        }

        //TODO: 插入到表中
        int startTime = (ByDateHelper.getCurrentTimeStamp().intValue());
        HttpServletRequest request = getRequest();
        String ip = "127.0.0.1";

        if(request != null) {
            ip = ByIpHelper.getLocalIp(request);
        }

        System.out.println(ip);
        long longIp = ByIpHelper.ip2long(ip);

        model = new SecurityCodeModel();
        model.setType(codeType);
        model.setClientId(this.getRequestData().getClientId());
        model.setStarttime(startTime);
        model.setEndtime(startTime + 5*60);
        model.setIp(longIp);
        model.setAccepter(accepter);
        model.setStatus(0);
        model.setCode(code);

        int lastInsertId = securityCodeService.insert(model);

        //TODO: 可以异步调用 发送 短信

        //返回消息
        if(lastInsertId > 0){
            return success(model.getCode());
        }

        return fail("获取失败");
    }

}
